//
//  JFGlobalInstance.m
//  JFPersonPage
//
//  Created by Lvjianfeng on 15/5/27.
//  Copyright (c) 2015年 famnotes. All rights reserved.
//
#import "JFGlobalInstance.h"

@implementation JFGlobalInstance

+ (id)shareIntance
{
    static JFGlobalInstance *shareJFGlobalInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shareJFGlobalInstance = [[self alloc] init];
    });
    
    return shareJFGlobalInstance;
}

//hidden cell spliteline
+ (void)setExtraCellLineHidden: (UITableView *)tableView{
    UIView *view =[ [UIView alloc]init];
    view.backgroundColor = [UIColor clearColor];
    [tableView setTableFooterView:view];
    [tableView setTableHeaderView:view];
}

+ (void)setTextFieldCustom:(UITextField*)textField textColor:(UIColor*)textFieldColor placehoder:(NSString*)placehoderStr{
    textField.backgroundColor = [UIColor whiteColor];
    textField.borderStyle = UITextBorderStyleRoundedRect;
    textField.textColor = textFieldColor;
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    textField.returnKeyType = UIReturnKeySearch;
    textField.placeholder = placehoderStr;
    [textField setValue:[UIColor customLightPlaceColor] forKeyPath:@"_placeholderLabel.textColor"];
}

+ (CGRect)boundingRectWithSize:(NSString *)text maxSize:(CGSize)maxSize font:(UIFont *)labelFont{
    return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObjectsAndKeys:labelFont,NSFontAttributeName, nil] context:nil];
}

+ (CGSize)sizeWithString:(NSString *)str font:(UIFont *)font maxSize:(CGSize)maxSize
{
    NSDictionary *dict = @{NSFontAttributeName : font};
    if (isNullString(str)) {
        return CGSizeMake(10, 10);
    }
    CGSize size =  [str boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size;
    return size;
}

+ (UIImageView *)setupNavigationTitleView{
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
    imageView.image = [UIImage imageNamed:@"logo_40"];
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    imageView.backgroundColor = [UIColor clearColor];
    return imageView;
}

+ (void)showAlertLabel:(UIView*)view text:(NSString*)text afterDelay:(NSTimeInterval)delay{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.mode = MBProgressHUDModeText;
    hud.labelText = text;
    hud.margin = 10.f;
    hud.removeFromSuperViewOnHide = YES;
    [hud hide:YES afterDelay:delay];
}

+ (BOOL)validateNumber:(NSString*)number {
    BOOL res = YES;
    NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
    int i = 0;
    while (i < number.length) {
        NSString * string = [number substringWithRange:NSMakeRange(i, 1)];
        NSRange range = [string rangeOfCharacterFromSet:tmpSet];
        if (range.length == 0) {
            res = NO;
            break;
        }
        i++;
    }
    return res;
}
+ (BOOL)validateEmail:(NSString *)email {
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:email];
}

+ (NSString *)trimSpace:(NSString *)strValue{
    return [strValue stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}


+ (NSString *)replaceString:(NSString *)value replaceValue:(NSString *)rValue{
    if (isNullString(value)) {//value == [NSNull null]
        return nil;
    }
    
    return [value stringByReplacingOccurrencesOfString:rValue withString:@""];
}

//去除空格（也可以替换）
+ (NSString *)removeSpaceOrreplace:(NSString *)strValue replaceValue:(NSArray *)reValue withStr:(NSString *)withStr
{
    if (withStr == nil && reValue == nil) {
        return [strValue stringByReplacingOccurrencesOfString:@" " withString:@""];
    }else{
        //NSArray *strReplace = [NSArray arrayWithObjects:@"-",@"+86",@"+1",@"(",@")",@" ",@"",nil];
        NSArray *strReplace = [NSArray arrayWithObjects:@"-",@"+86",@"+1",@"＋",@"(",@")", @" ",@" ",@"",nil];//@" "
        for (int i=0; i<strReplace.count;i++) {
            strValue = [strValue stringByReplacingOccurrencesOfString:strReplace[i] withString:withStr];
        }
        return strValue;
    }
}

- (NSString *)appSystemFont:(NSString *)fontPlistName
{
    NSString *filename = [self loadPlistFile:fontPlistName];
    if (filename) {
        NSString *font = [[NSDictionary dictionaryWithContentsOfFile:filename] objectForKey:@"SysFont"];
        return font;
    }
    
    return @"HelveticaNeue-Thin";//@"Avenir-Light";
}

- (NSString *)loadPlistFile:(NSString *)fileNameWithoutExtension
{
    NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *plistPath = [pathArray objectAtIndex:0];
    NSString *filename=[plistPath stringByAppendingPathComponent:[fileNameWithoutExtension stringByAppendingString:@".plist"]];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:filename]){
        return filename;
    }
    return nil;
}
/*
 NSDate *selected = [datePicker date];
 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
 [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
 NSString *destDateString = [dateFormatter stringFromDate:selected];
 */
#pragma mark 时间转换

+ (NSString *)StringFormatDateFromTimeStamp:(NSInteger)timeStamp
{
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeStamp];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"MM-dd-yyyy"];
    NSString *sDate = [dateFormatter stringFromDate:date];
    return sDate;
}

+ (NSString *)StringFormatDate:(NSDate *)date
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"yyyy年MM月dd日"];
    NSString *locationDate = [dateFormatter stringFromDate:date];
    return locationDate;
}

+ (int)DateConvertToAge:(NSDate *)date
{
    int age = [self getYear:[NSDate date]] - [self getYear:date];
    if (age < 0) {
        age = 0;
    }
    return age;
}

+ (NSString *)StringFormatDate_NoLine:(NSDate *)date{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"yyyyMMdd"];
    NSString *dateString = [dateFormatter stringFromDate:date];
    return dateString;
}

+ (int)getYear:(NSDate *)date{
    NSString *dateString = [self StringFormatDate_NoLine:date];
    int year = [[dateString substringToIndex:4] intValue];
    return year;
}

+ (int)getMonth:(NSDate *)date{
    NSString *dateString = [self StringFormatDate_NoLine:date];
    int month = [[dateString substringWithRange:NSMakeRange(4, 2)] intValue];
    //int day = [[dateString substringFromIndex:6] intValue]
    return month;
}

+ (int)getDay:(NSDate *)date{
    NSString *dateString = [self StringFormatDate_NoLine:date];
    int day = [[dateString substringFromIndex:6] intValue];
    return day;
}

//线
+ (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size

{
    CGRect rect = CGRectMake(0, 0, size.width, size.height);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context,color.CGColor);
    CGContextFillRect(context, rect);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}
@end
